package offer;

import java.util.HashMap;

public class offer39 {
    public static int majorityElement(int[] nums) {
        int len = nums.length;
        int mid = len/2+1;


        HashMap<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i <len ; i++) {
            if(map.containsKey(nums[i])){
            int value = map.get(nums[i]);
            if(value==0)
            {
                value=1;
            }else
                value++;
            map.put(nums[i],value);
                }
            else
                map.put(nums[i],1);
        }
        for(Integer k : map.keySet())
        {
            if(map.get(k)>=mid)
            {
                return k;
            }
        }
       return 0;
    }

    public static void main(String[] args) {
        int []nums = {3,2,3};
        majorityElement(nums);
    }
}
